﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using System.ServiceModel;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
	public partial class BatchUpdateRoleStatus : Microsoft.Practices.CompositeWeb.Web.UI.Page, IBatchUpdateRoleStatusView
	{
		private BatchUpdateRoleStatusPresenter _presenter;

        #region Property

        public string RoleName
        {
            get
            {
                return this.txtrolename.Text.ToString().Trim();
            }
        }


        public int StatusId
        {
            get
            {
                return int.Parse(this.DropDownList1.SelectedValue);
            }

        }

        public string StatusName
        {
            get
            {
                return this.DropDownList1.SelectedItem.Text.Trim();
            }
        }

        public IList<StatusDTO> StatusList
        {
            set
            {
                this.DropDownList1.DataSource = value;
                this.DropDownList1.DataTextField = "StatusName";
                this.DropDownList1.DataValueField = "StatusId";
                this.DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("select", "0"));
            }
        }

        public IList<RoleDTO> RoleDetailsList
        {
            set
            {
                this.rptlist.DataSource = value;
                this.rptlist.DataBind();
            }
        }

       
        private List<int> _selectroleid = new List<int>();
        public List<int> Selectroleid
        {
            set { _selectroleid = value; }
            get
            {


                for (int i = 0; i < this.rptlist.Items.Count; i++)
                {
                    CheckBox chb = (rptlist.Items[i].FindControl("chks")) as CheckBox;

                    if (chb.Checked == true)
                    {
                        int roleId = int.Parse(((rptlist.Items[i].FindControl("HiddenField1")) as HiddenField).Value);
                        this._selectroleid.Add(roleId);
                    }
                }

                return _selectroleid;
            }

        }
        private DataContracts.RoleDTO _roleDTO;

        public DataContracts.RoleDTO roleDTO
        {

            get
            {

                return _roleDTO;

            }

            set
            {

                _roleDTO = value;

            }

        }

        #endregion



        public void ShowOperationMessage(string message)
        {
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }


        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            if (e.CommandName == "del")
            {
                _presenter.DeleteRole(id);
                _presenter.OnViewInitialized();
                _presenter.GetAllRole(RoleName, StatusId);
              
            }
            if (e.CommandName == "upt")
            {
                _presenter.UpdateCheck(id);
                _presenter.OnViewInitialized();
                _presenter.GetAllRole(RoleName, StatusId);
               
            }

        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Button b1 = (Button)e.Item.FindControl("btndel");

            if (b1 != null)
            {
                b1.Attributes.Add("onclick", "return confirm(\"Do you want to delete it?\");");
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
                _presenter.GetAllRole(RoleName,StatusId);
            
            }
        }

        [CreateNew]
        public BatchUpdateRoleStatusPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        protected void btninuse_Click(object sender, EventArgs e)
        {

            _presenter.BatchInUse();
            _presenter.OnViewInitialized();
            _presenter.GetAllRole(RoleName, StatusId);
          
        }

        protected void btnnouse_Click(object sender, EventArgs e)
        {

            _presenter.BatchNoUse();
            _presenter.OnViewInitialized();
            _presenter.GetAllRole(RoleName, StatusId);
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            _presenter.GetAllRole(RoleName,StatusId);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("SearchRole.aspx");
        }
        protected void addrole_Click1(object sender, EventArgs e)
        {
            Response.Redirect("RoleDetail.aspx");
        }

        public void Redirect(string url)
        {
            Response.Redirect(url);
        }
    }
}

